<!DOCTYPE html>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href='http://fonts.googleapis.com/css?family=Arvo'
	rel='stylesheet' type='text/css'>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
	<h1>SMS Attendance</h1>
	<h2>Make your Selection</h2>
	<form action="index.php" method="post">
		View All Messages <input type="radio" name="all" value="messages" /><br />
		View All Keywords <input type="radio" name="all" value="keywords" /><br />
		View All Members <input type="radio" name="all" value="members" /><br />
		<input type="submit" " />
	</form>
	<form name="input" action="index.php" method="post">
		View Members For a Keyword <input type="text"
			name="members_keywords" /> <input type="submit" />
	</form>
	<form name="input" action="index.php" method="post">
		View An Individual Person<input type="text" name="person_firstName"/> <input type="text" name="person_lastName"/>
		<input type="submit" />
	</form>


	<?php 
	session_start();

	//connect to database and update
	$db_server = 'localhost:3306';
	$db_user = 'root';
	$db_password = '1234';
	$db_dbname = 'smsAttendance';
	$db = mysql_pconnect($db_server, $db_user, $db_password);
	mysql_select_db($db_dbname);

	//deciding based off of the input
	//radio buttons
	if (isset($_POST['all'])){
		switch($_POST['all']){//which radio button
			case "messages":
				//display messages
				echo "<h2>Messages</h2>";
				echo "<table border='1'>";
				$result = mysql_query("SELECT * FROM messages");
				while($row = mysql_fetch_array($result)){
					echo "<tr><td>";
					echo $row['Phone'] . "<td>" . $row['Message'];
					echo "</td></tr>";
				}
				echo "</table>";
				break;
			case "keywords":
				echo "<h2>Keywords</h2>";
				echo "<table border='1'>";
				$result = mysql_query("SELECT * FROM keywords");
				while($row = mysql_fetch_array($result)){
					echo "<tr><td>";
					echo $row['KeyWord'];
					echo "</td></tr>";
				}
				echo "</table>";
				break;
			case "members":
				//display events
				echo "<h2>Events</h2>";
				echo "<table border='1'>";
				$result = mysql_query("SELECT * FROM members");
				while($row = mysql_fetch_array($result)){
					echo "<tr><td>";
					echo $row['FirstName'] . "<td>" . $row['LastName'] . "<td>" . $row['Phone'] . "<td>" . $row['Email'];
					echo "</td></tr>";
				}
				echo "</table>";
				break;
		}
	}
	//members at a keyword
	if (isset($_POST['members_keywords'])){
		echo "<h2>Members at a keyword ". $_POST["members_keywords"] ."</h2>";
		echo "<table border='1'>";
		$query = "	SELECT M.FirstName, M.LastName, M.Email, M.Phone
		FROM members M, messages E
		WHERE 	M.Phone = E.Phone
		AND TRIM(E.Message) LIKE '".$_POST["members_keywords"]."'";
		//echo $query;
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			echo "<tr><td>";
			echo $row['Phone'] . "<td>" . $row['FirstName'] . "<td>"  . $row['LastName'] . "<td>"  . $row['Email'];
			echo "</td></tr>";
		}
		echo "</table>";
	}
	// person
	if (isset($_POST["person_firstName"])&&isset($_POST["person_lastName"])){
		$name = $_POST["person_firstName"]." ".$_POST["person_lastName"];
		//echo $name;
		echo "<h2>Individual Person: ". $name ."</h2>";
		
		// Person information
		echo "<table border='1'>";
		$query = 	"SELECT FirstName, LastName, Phone, Email
		FROM members
		WHERE 	members.FirstName LIKE '".$_POST["person_firstName"]."'
		AND 	members.LastName LIKE '".$_POST["person_lastName"]."'";
		//echo $query;
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			echo "<tr><td>";
			echo $row['FirstName'] . "<td>" . $row['LastName'] . "<td>" . $row['Phone'] . "<td>" . $row['Email'];
			echo "</td></tr>";
		}
		echo "</table>";
		
		// Keywords
		echo "<table border='1'>";
		$query = 	"SELECT keywords.KeyWord
		FROM messages, members, keywords
		WHERE 	members.FirstName LIKE '".$_POST["person_firstName"]."' 
		AND 	members.LastName LIKE '".$_POST["person_lastName"]."' 
		AND 	members.Phone = messages.Phone
		AND 	keywords.KeyWord = messages.Message";
		//echo $query;
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)){
			echo "<tr><td>";
			echo $row['KeyWord'];
			echo "</td></tr>";
		}
		echo "</table>";
	}
	
	
	mysql_close();
	?>


	<h5>
		<br>&copy; 2013 Brian Goodacre
	</h5>
</body>
</html>
